<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

class AdController extends BaseController {

    /**
     * 广告页
     */
    public function ad() {
        $act = I('GET.act', 'add');
        $adId = I('GET.ad_id');
        $adArr = array();
        if ($adId) {
            $adArr = D('ad')->where('ad_id=' . $adId)->find();
            $adArr['start_time'] = date('Y-m-d', $adArr['start_time']);
            $adArr['end_time'] = date('Y-m-d', $adArr['end_time']);
        }
        if ($act == 'add')
            $adArr['pid'] = $_GET['pid'];
        $position = D('ad_position')->where('1=1 and is_open=1')->select();
        $this->assign('info', $adArr);
        $this->assign('act', $act);
        $this->assign('position', $position);
        $this->display();
    }

    /**
     * 广告列表
     */
    public function adList() {
        delFile(RUNTIME_PATH . 'Html'); // 先清除缓存, 否则不好预览
        $adMod = M('ad');
        $where = "1=1";
        if (I('pid')) {
            $where = "pid=" . I('pid');
            $this->assign('pid', I('pid'));
        }
        $keywords = I('keywords', false);
        if ($keywords) {
            $where = "ad_name like '%$keywords%'";
        }
        $count = $adMod->where($where)->count();
        $Page = new \Think\Page($count, 10);
        $adArr = $adMod->where($where)->order('pid desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $list = array();
        if ($adArr) {
            $media = array('图片', '文字', 'flash');
            foreach ($adArr as $val) {
                $val['media_type'] = $media[$val['media_type']];
                $list[] = $val;
            }
        }

        $goodsCatRs= M('GoodsCat')->where(array('level' => 1))->field('id,name')->select();
        $this->assign('goodsCatList',$goodsCatRs);

        $adPositionRs = M('AdPosition')->getField("position_id,position_name,is_open");
        $this->assign('ad_position_list', $adPositionRs); //广告位
        $show = $Page->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 广告位置
     */
    public function position() {
        $act = I('GET.act', 'add');
        $positionId = I('GET.position_id');
        $info = array();
        if ($positionId) {
            $info = D('ad_position')->where('position_id=' . $positionId)->find();
            $this->assign('info', $info);
        }
        $this->assign('act', $act);
        $this->display();
    }

    /**
     * 广告位置列表
     */
    public function positionList() {
        $AdPositionMod = M('ad_position');
        $count = $AdPositionMod->where('1=1')->count();
        $Page = new \Think\Page($count, 10);
        $list = $AdPositionMod->order('position_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('list', $list);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 广告处理
     */
    public function adHandle() {
        $post = I('post.');
        $post['start_time'] = strtotime($post['begin']);
        $post['end_time'] = strtotime($post['end']);

        if ($post['act'] == 'add') {
            $r = D('ad')->add($post);
        }
        if ($post['act'] == 'edit') {
            $r = D('ad')->where('ad_id=' . $post['ad_id'])->save($post);
        }

        if ($post['act'] == 'del') {
            $r = D('ad')->where('ad_id=' . $post['del_id'])->delete();
            if ($r)
                exit(json_encode(1));
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Ad/adList');
        // 不管是添加还是修改广告 都清除一下缓存
        delFile(RUNTIME_PATH . 'Html'); // 先清除缓存, 否则不好预览

        if ($r) {
            $this->success("操作成功", U('Admin/Ad/adList'));
        } else {
            $this->error("操作失败", $referurl);
        }
    }

    /**
     * 广告位置处理
     */
    public function positionHandle() {
        $post = I('post.');
        if ($post['act'] == 'add') {
            $r = M('ad_position')->add($post);
        }

        if ($post['act'] == 'edit') {
            $r = M('ad_position')->where('position_id=' . $post['position_id'])->save($post);
        }

        if ($post['act'] == 'del') {
            if (M('ad')->where('pid=' . $post['position_id'])->count() > 0) {
                $this->error("此广告位下还有广告，请先清除", U('Admin/Ad/positionList'));
            } else {
                $r = M('ad_position')->where('position_id=' . $post['position_id'])->delete();
                if ($r)
                    exit(json_encode(1));
            }
        }
        $referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Ad/positionList');
        if ($r) {
            $this->success("操作成功", $referurl);
        } else {
            $this->error("操作失败", $referurl);
        }
    }

    /**
     * 修改广告
     */
    public function changeAdField() {
        $post[$_REQUEST['field']] = I('GET.value');
        $post['ad_id'] = I('GET.ad_id');
        M('ad')->save($post);
    }

}
